#!/bin/bash

# https://www.elastic.co/guide/en/elasticsearch/reference/master/put-transform.html
curl -X DELETE "localhost:9200/aggstest_agg"
curl -X DELETE "localhost:9200/aggstest"
curl -X DELETE "localhost:9200/_transform/aggstest"

 curl -XPUT "localhost:9200/aggstest" -H 'Content-Type: application/json' -d'
{
  "mappings": {
      "properties": {
        "bytes": {
          "type": "long"
        },
        "clientip": {
          "type": "ip"
        },
        "db":{
          "type": "keyword"
        },
        "timestamp": {
          "type": "date"
        }
      }
    }
}
'
# 历史名字的删除有问题？？造成后续的聚合不对
curl -X PUT "localhost:9200/_transform/aggstest"  -H 'Content-Type: application/json' -d'
{
  "source": {
    "index": [
      "aggstest"
    ]
  },
  "pivot": {
    "group_by": {
      "clientip": {
        "terms": {
          "field": "clientip"
        }
      }
    },
    "aggregations": {
      "bytes.sum": {
        "sum": {
          "field": "bytes"
        }
      },
      "db.cardinality": {
        "cardinality": {
          "field": "db"
        }
      },
      "db.value_count": {
        "value_count": {
          "field": "db"
        }
      }
    }
  },
  "dest": {
    "index": "aggstest_agg"
  },
  "sync": {
    "time": {
      "field": "timestamp",
      "delay": "1s"
    }
  },
  "frequency": "1s"
}"
'
#
curl -XPOST "localhost:9200/_transform/aggstest/_start?pretty"

exit 1
# https://www.elastic.co/guide/en/elasticsearch/reference/master/transform-checkpoints.html
curl -XPOST "localhost:9200/aggstest/_doc" -H 'Content-Type: application/json' -d'
{ "clientip": "10.10.10.1" ,"bytes":1, "db": "system", "timestamp": "2020-11-11T08:15:53.821Z" }
'
curl -XPOST "localhost:9200/aggstest/_doc" -H 'Content-Type: application/json' -d'
{ "clientip": "10.10.10.1" ,"bytes":1, "db": "system", "timestamp": "2020-11-11T08:15:53.821Z" }
'
:<<EOF
GET aggstest/_search
{
  "query": {
    "match_all": {}
  }
}

GET aggstest_agg/_search
{
  "query": {
    "match_all": {}
  }
}
          "bytes" : {
            "sum" : 2.0
          },
          "clientip" : "10.10.10.1",
          "db" : {
            "cardinality" : 1.0,
            "value_count" : 2.0
          }
EOF

#---
 curl -XPOST "localhost:9200/aggstest/_doc" -H 'Content-Type: application/json' -d'
{ "clientip": "10.10.10.1" ,"bytes":1, "db": "sys", "timestamp": "2020-11-12T08:15:53.821Z" }
'
 curl -XPOST "localhost:9200/aggstest/_doc" -H 'Content-Type: application/json' -d'
{ "clientip": "10.10.10.2" ,"bytes":1, "db": "sys", "timestamp": "2020-11-12T08:15:53.821Z" }
'
curl -XPOST "localhost:9200/aggstest/_doc" -H 'Content-Type: application/json' -d'
{ "clientip": "10.10.10.1" ,"bytes":1, "db": "system", "timestamp": "2020-11-12T08:15:53.821Z" }
'

#---
curl -XPOST "localhost:9200/aggstest/_doc" -H 'Content-Type: application/json' -d'
{ "clientip": "10.10.10.1" ,"bytes":1, "db": "system", "timestamp": "2020-11-13T08:15:53.821Z" }
'
curl -XPOST "localhost:9200/aggstest/_doc" -H 'Content-Type: application/json' -d'
{ "clientip": "10.10.10.2" ,"bytes":1, "db": "system", "timestamp": "2020-11-13T08:15:53.821Z" }
'
curl -XPOST "localhost:9200/aggstest/_doc" -H 'Content-Type: application/json' -d'
{ "clientip": "10.10.10.2" ,"bytes":1, "db": "system", "timestamp": "2020-11-14T08:15:53.821Z" }
'
curl -XPOST "localhost:9200/aggstest/_doc" -H 'Content-Type: application/json' -d'
{ "clientip": "10.10.10.2" ,"bytes":1, "db": "system", "timestamp": "2020-11-15T08:15:53.821Z" }
'

#curl "http://localhost:9200/aggstest/_search?pretty"
#curl "http://localhost:9200/aggstest_agg/_search?pretty"

